﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using LoveMusic.Models;
namespace Dal
{

    public class DISCOGZ
    {
        private DiscogsAPI.ResponseClasses.ArtistRequest.artist ARTIST
        {
            get;
            set;
        }

        public string API_KEY
        {
            private get;
            set;
        }

        /// <summary>
        /// Return artist info
        /// </summary>
        /// <returns></returns>
        public DiscogsAPI.ResponseClasses.ArtistRequest.artist FindArtist(string artistName)
        {
            DiscogsAPI.Discogs.API_KEY = API_KEY;
            return DiscogsAPI.Discogs.SearchArtist(artistName).artist;
        }

        public List<IMAGE> GetImages(string artistName, int artistID)
        {
            List<IMAGE> image = new List<IMAGE>();
            try
            {
                artistName = artistName.Replace(" ", "+");
                string xml = LoveMusic.Helpers.WebHelper.GetResponse("http://www.discogs.com/artist/" + artistName + "?f=xml&api_key=" + API_KEY);
                if (xml==null)
                    xml = LoveMusic.Helpers.WebHelper.GetResponseStandard("http://www.discogs.com/artist/" + artistName + "?f=xml&api_key=" + API_KEY);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new StringReader(xml));
                foreach (XmlElement element in xmlDoc.GetElementsByTagName("image"))
                {
                    try
                    {
                        IMAGE item = new IMAGE();
                        item.ARTIST_IDZ = artistID;
                        if (element.Attributes["height"].Value != null && element.Attributes["height"].Value != string.Empty)
                            item.HEIGHT = Int32.Parse(element.Attributes["height"].Value);

                        if (element.Attributes["width"].Value != null && element.Attributes["width"].Value != string.Empty)
                            item.WIDTH = Int32.Parse(element.Attributes["width"].Value);

                        if (element.Attributes["type"].Value != null && element.Attributes["type"].Value != string.Empty)
                            item.TYPE = element.Attributes["type"].Value;

                        if (element.Attributes["uri"].Value != null && element.Attributes["uri"].Value != string.Empty)
                            item.URL = element.Attributes["uri"].Value;

                        if (element.Attributes["uri150"].Value != null && element.Attributes["uri150"].Value != string.Empty)
                            item.URLTHUMB = element.Attributes["uri150"].Value;
                        image.Add(item);
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
                return image;
            }
            catch (Exception ex)
            {// No pictures to return return empty list
                return image;
            }
        }
    }
}